<?php
/**
 * Created by PhpStorm.
 * Script Name: Minitj.php
 * Create: 2023/2/6 9:35
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\tenant\controller;

use EasyOpen\Tencent\Client as TencentCli;
use EasyOpen\Tencent\Request\Mp\DataCubeGetWeAnalysisAppidDailyVisitTrend;
use plugin\wechat\app\service\Mini as MiniService;
use plugin\wechat\app\TenantController;
use Support\Request;

class Minitj extends TenantController
{
    /**
     * @var TencentCli
     */
    private $client;

    public function __construct()
    {
        parent::__construct(); // TODO: Change the autogenerated stub
        $this->client = new TencentCli();
    }

    public function index(Request $request){
        $mini_info = $request->miniInfo;
        $access_token = MiniService::getAccessToken();

        $yesterday_date = date('Ymd', strtotime('-1 days', time()));
        $yesterday = [
            'session_cnt' => 0, //打开次数
            'visit_pv' => 0,  //访问次数
            'visit_uv' => 0, //访问人数
            'visit_uv_new' => 0, //新用户数
            'stay_time_uv' => 0,  //人均停留时长(秒)
            'stay_time_session' => 0 //次均停留时长(秒)
        ];

        if($mini_info['is_auth']){
            $request = new DataCubeGetWeAnalysisAppidDailyVisitTrend();
            $request->setBeginDate($yesterday_date);
            $request->setEndDate($yesterday_date);
            $response = $this->client->execute($request, $access_token);
            if(isset($response['list'])){
                if(count($response['list'])){
                    $yesterday = $response['list'][0];
                }
            }
        }

        $this->assign('yesterday', $yesterday);
        return $this->show();
    }
}